import levin.lib.levinCommon as levinCommon
import gettext
gettext.install('LeVin', levinCommon.LEVIN_PO, unicode=True)

import sys

if True :
    levinCommon.USER_SAVE = 'user3.pk'
    levinCommon.DEBUG_MODE = True

from kivy.app import App
from kivy.config import Config
#Config.set('graphics', 'width', 1000)
#Config.set('graphics', 'height', 800)
Config.read('config.ini')

import levin.lib.guiCommon as guiCommon
import levin.lib.wineAreas as wineAreas
import levin.lib.wineAreasWidget as wineAreasWidget

## wineAreasWidget.initWidgets()


## import levin.lib.mainWindow as mainWindow
## from kivy.core.window import Window

## #Window.size = (1000,1000)

## theTree = wineAreasWidget.theTree

## wineWorld = wineAreas.WorldArea.defaultWorld
## mWindow = mainWindow.MainWindow(_mainListContainer=wineWorld)
## mWindow._mainListContainer.add_widget(theTree)

## import levin.lib.wineDisplay as wineDisplay
## wDisplay = wineDisplay.WineDisplay()
## mWindow._sidepanel.bottleTab.add_tab(_('Description'),wDisplay)

## import levin.lib.tastingDisplay as tastingDisplay
## tDisplay = tastingDisplay.TastingDisplay()
## mWindow._sidepanel.bottleTab.add_tab(_('Tastings'),tDisplay)
## tDisplay.tastingList.setDebugPrefix('tDisplay')

## from levin.lib.sidePanel import AreaDisplay
## aDisplay = AreaDisplay()
## mWindow._sidepanel.areaTab.add_tab(_('Map'), aDisplay)

## def wineNodeSelected(o,node):
##     print ' wineNodeSelected  ', type(node)
##     mWindow._sidepanel.selectedObject = node
##     if isinstance(node,wineAreas.Bottle):
##         guiCommon.commonEvents.selectedBottle = node

    
## theTree.bind(_selected_node=wineNodeSelected)

## # import edit capabilities
## import levin.lib.wineEdit
## import levin.lib.tastingEdit
## import levin.lib.tastingList as tastingList

## def tastingNodeSelected(o,node):
##     #i=tastingList.tastingListWidget.selected_index()
##     #b = tastingList.tastingListWidget.tastings[i].bottle
##     b = tastingList.tastingListWidget._selected_node.tasting.bottle
##     mWindow._sidepanel.selectedObject = b
##     guiCommon.commonEvents.selectedBottle = b

    
## def switch_to_tastinglist_mode(*l):
##     if tastingList.tastingListWidget is None:
##         tastingList.tastingListWidget  = tastingList.TastingList()
##         tastingList.tastingListWidget.populate()
##         tastingList.tastingListWidget.bind(_selected_node = tastingNodeSelected)
##     mWindow._mainListContainer.clear_widgets()
##     mWindow._mainListContainer.add_widget(tastingList.tastingListWidget)

## def switch_to_cellar_mode(*l):
##     mWindow._mainListContainer.clear_widgets()
##     mWindow._mainListContainer.add_widget(theTree)

## guiCommon.commonEvents.bind( on_cellar_mode = switch_to_cellar_mode, on_tastinglist_mode = switch_to_tastinglist_mode)

from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

class MyButton(Button):
    pass

b = guiCommon.LeVinButton(text = 'dasdsad')
b2 = guiCommon.LeVinButton(text = 'just sync')
#b = MyButton(text = 'dasdsad')



import tests.noguiTest
from levin.lib.synchroGui import resolveByGui, start_synchro_choosing_file
sTest = tests.noguiTest.SynchroTest( resolveByGui,w0IsWorldW= True)

lay = BoxLayout(orientation = 'vertical')
#b.bind(on_press = sTest.fullSyncTest )

def retest(*l):
    #sTest.unsync()
    sTest.setup()
    sTest.w1.saveUserContent(sTest.w1._user_content)
    print ' cccccccc w0 ',len(sTest.w0._user_cepages), 'w1:', len(sTest.w1._user_cepages)
    start_synchro_choosing_file()
    #sTest.sync()
b2.bind(on_press = retest )

lay.add_widget(b)
lay.add_widget(b2)

# define the app ---------------------
class LeVinApp(App):
    def build_config(self, config):
        config.setdefaults('Paths', {
            "Images" : levinCommon.USER_HOME

            })
        ## config.setdefaults('bla', {
        ##     'width': 1000,
        ##     'height': 800,
        ##     'key1': 'value1',
        ## })

    def build(self):
        #self.config.set('graphics', 'width', 1000)
        #self.config.set('graphics', 'height', 800)        
        #self.mWindow = mWindow
        return lay
        #return mWindow

    def get_application_config(self,  *l):
        return levinCommon.USER_CONFIG
ap=LeVinApp()
levinCommon.application = ap

# ------------------------------------

guiCommon.load_kv_file('overwrite.kv',now=True)

if __name__ == '__main__':
    #interactiveKivy.setupInteractive( ap, locals())
    ap.run()
